package com.work.example.demo5.controller;

import com.work.example.demo5.domain.CategoryEntity;
import com.work.example.demo5.service.CategoryService;
import com.work.example.demo5.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin    //跨域
@RestController  //RESTFUL API
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping("/getcategories")
    public R getCategories() {
        return R.success(categoryService.list());
    }

    @GetMapping("/getcategoryId")
    public R getAuthorId(String name) {
        return R.success(categoryService.getcategoryId(name));
    }

    @GetMapping("/delete")
    public R delete(@RequestParam("id") Long id) {
        System.out.println(id);
        return R.success(categoryService.removecategory(id));
    }

    @GetMapping("/add")
    public R add(@RequestParam("name") String name) {
        return R.success(categoryService.save(new CategoryEntity(null, name)));
    }
    @GetMapping("/updatecategory")
    public R update(@RequestParam("categoryName") String categoryName,
                    @RequestParam("categoryId") Long categoryId) {
        CategoryEntity categoryEntity = new CategoryEntity(categoryId, categoryName);
        System.out.println(categoryEntity);
        return R.success(categoryService.updatecategory(categoryEntity));
    }
}
